package com.study.edoc.exception;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 自定义返回给前端统一数据结构
 */
@ApiModel(value = "com.study.edoc.exception.R",description = "响应信息描述")
public class R implements Serializable {

    @ApiModelProperty(value = "响应状态码")
    private int code;

    @ApiModelProperty(value = "响应提示信息")
    private String msg;

    @ApiModelProperty(value = "响应数据")
    private Object data;

    public R() {
    }

    public R(BaseErrorInfo errorInfo, Object data) {
        this.code = errorInfo.getCode();
        this.msg = errorInfo.getMsg();
        this.data = data;
    }

    public static R success(Object data){
        R res = new R();
        res.setCode(ResultCodeEnum.SUCCESS.getCode());
        res.setMsg(ResultCodeEnum.SUCCESS.getMsg());
        res.setData(data);
        return res;
    }

    public static R success(int code,String msg){
        R res = new R();
        res.setCode(code);
        res.setMsg(msg);
        res.setData(null);
        return res;
    }

    public static R error(int code,String msg){
        R res = new R();
        res.setCode(code);
        res.setMsg(msg);
        res.setData(null);
        return res;
    }

    public static R error(BaseErrorInfo errorInfo){
        R res = new R();
        res.setCode(errorInfo.getCode());
        res.setMsg(errorInfo.getMsg());
        res.setData(null);
        return res;
    }

    public static R fail(String msg){
        R res = new R();
        res.setCode(-1);
        res.setMsg(msg);
        res.setData(null);
        return res;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
